# Copyright (C) Nicola Bizzoca   2006/2007  <hannibal_smith@users.sourceforge.net>
# Copyright (C) Simone Economo   2006/2007  <ekoes@users.sourceforge.net>
# 
#    This file is part of Absynth-FTP.
#
#    Absynth-FTP is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    Absynth-FTP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gtk, TestSuite

class itemStore:
    
    def __init__(self, toolitem, item, tooltip):
        self.toolitem, self.item, self.tooltip = toolitem, item, tooltip
        
    def getTooltip(self):
        return gtk.tooltips_data_get(self.toolitem)[2]
        
""" class radioGroupStore:
    
    def __init__(self, id, item):
        self.root = {id: item}
        self.children = {}
    
    def addChild(self, id, item):
        self.children[id] = item
        
    def getRoot(self):
        return self.root.values()[0] """

class genericToolBar:
    
    def __init__(self, features):
        self.__features = features
        self.count = {"items": 0}
        self.items = {}
        self.tooltips = gtk.Tooltips()
        #self.radios = {}
        self.widget = gtk.Toolbar()
        methods = {
            "orientation": "set_orientation",
            "tooltips"   : "set_tooltips",
            "layout"     : "set_style",
        }
        self.__setOrientationMode(self.__features["orientation"])
        self.__setLayoutMode(self.__features["layout"])
        for property, value in self.__features.items():
            getattr(self.widget, methods[property])(value)
        
    def __setOrientationMode(self, mode):
        orientation = {
            "hor": gtk.ORIENTATION_HORIZONTAL,
            "ver": gtk.ORIENTATION_VERTICAL,
        }
        self.__features["orientation"] = orientation[mode]
        
    def __setLayoutMode(self, mode):
        layout = {
            "icon": gtk.TOOLBAR_ICONS,
            "text": gtk.TOOLBAR_TEXT,
            "both": gtk.TOOLBAR_BOTH,
            "side": gtk.TOOLBAR_BOTH_HORIZ,
        }
        self.__features["layout"] = layout[mode]
        
    def addItem(self, item, desc = None, position = None):
        toolitem = gtk.ToolItem()
        toolitem.add(item)
        self.__appendNewItem(toolitem, item, desc, position)
        
    def addSimpleButton(self, label, desc = None, position = None):
        self.addItem(gtk.Button(label), desc)
        
    def addImageButton(self, image, label = None, desc = None, position = None):
        if label is None:
            toolitem = gtk.ToolButton(image)
        else:
            if not callable(image):
                image = gtk.image_new_from_stock(image, gtk.ICON_SIZE_MENU)
            toolitem = gtk.ToolButton(image, label)
        self.__appendNewItem(toolitem, None, desc, position)
        
    def addToggleButton(self, stock, desc = None, position = None, callback = None, active = True):
        toolitem = gtk.ToggleToolButton(stock)
        toolitem.set_active(True)
        toolitem.connect("toggled", self.__onButtonToggled, callback)
        self.__appendNewItem(toolitem, None, desc, position)
        
    """ def addRadioButton(self, stock, group, position = None, callback = None, active = True ):
        item = gtk.RadioToolButton(None, stock)
        item.set_active(True)
        item.connect("toggled", self.__onButtonToggled, callback)
        itemstore = self.items[self.count["items"]] = itemStore(item, None)
        if group in self.radios.keys():
            item.set_group(self.radios[group].getRoot().item)
            self.radios[group].addChild(self.count["items"], itemstore)
        else:
            self.radios[group] = radioGroupStore(self.count["items"], itemstore)
        self.__appendNewItem(item, None, position, itemstore) """
            
    #def __appendNewItem(self, toolitem, item, position, itemstore = None):
        #self.items[self.count["items"]] = True and itemstore or itemStore(toolitem, item)
    def __appendNewItem(self, toolitem, item, desc, position):
        tooltip = toolitem.set_tooltip(self.tooltips, desc)
        self.items[self.count["items"]] = itemStore(toolitem, item, tooltip)
        self.count["items"] += 1
        if position is None:
            self.widget.add(toolitem)
        else:
            self.widget.insert(toolitem, position)
            
    def __onButtonToggled(self, widget, callback):
        if callable(callback): callback()
            
def nestedSuite():
    toolbar = genericToolBar({
        "orientation": "hor",
        "tooltips"   : True,
        "layout"     : "both",
    })
    toolbar.addSimpleButton("foo", "Esta noche")
    toolbar.addImageButton(gtk.STOCK_HOME, desc = "Esta noche")
    toolbar.addImageButton(gtk.STOCK_HOME, "caliente", "Esta noche")
    toolbar.addToggleButton(gtk.STOCK_OPEN, desc = "Esta noche")
    """ toolbar.addRadioButton(gtk.STOCK_NEW, "asd")
    toolbar.addRadioButton(gtk.STOCK_NEW, "asd")
    toolbar.addRadioButton(gtk.STOCK_NEW, "asd")
    print toolbar.items[3].toolitem """
    print toolbar.items, toolbar.items[0].toolitem
    return toolbar
            
if __name__ == "__main__":
    TestSuite.newSuite(nestedSuite).runTest()

    